const { sqlPromise } = require("../db/sqlPromise");

exports.getBooks = async (req, res) => {
  let sql = "select * from books";
  let results = await sqlPromise(sql);
  res.send({
    status: 200,
    data: results,
  });
};

exports.getBook = async (req, res) => {
  let sql = "select * from books where id=?";
  let id = req.params.id;
  let results = await sqlPromise(sql, [id]);
  res.send({
    status: 200,
    data: results,
  });
};

exports.addBook = async (req, res) => {
  let sql = "INSERT INTO books (name,author,publisher) VALUES (?,?,?)";
  let results = await sqlPromise(sql, [
    req.body.name,
    req.body.author,
    req.body.publisher,
  ]);
  res.send({
    status: 200,
    data: results,
  });
};

exports.delBook = async (req, res) => {
  let sql = "DELETE FROM books WHERE id=?";
  let id = req.params.id;
  let results = await sqlPromise(sql, [id]);
  res.send({
    status: 200,
    data: results,
  });
};

exports.updateBook = async (req, res) => {
  let sql = "UPDATE books SET ? WHERE id=?";
  let obj = {
    name: req.body.name,
    author: req.body.author,
    publisher: req.body.publisher,
  };
  let results = await sqlPromise(sql, [obj, req.body.id]);
  res.send({
    status: 200,
    data: results,
  });
};
